﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace ClipboardEx
{
    public enum EItemType
    {
        EIT_UNKNOW,
        EIT_FILE,
        EIT_TEXT,
        EIT_DIR
    }

    public class ListItemEx
    {
        public string Name
        { get; set; }
        public string Type
        { get; set; }
        public override string ToString()
        {
            return Name;
        }

        public EItemType EType
        {
            set
            {
                Type = ListboxEx.TypeString[(int)value];
                ItemImage = ListboxEx.Images[(int)value];
            }
            get
            {
                return EType;
            }
        }
        public Image ItemImage
        {
            get;
            set;
        }

        public override bool Equals(object obj)
        {
            ListItemEx r = obj as ListItemEx;
            if (r == null)
            {
                return false;
            }
            return Name == r.Name && Type == r.Type;
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public ListItemEx(string name)
        {
            Name = name;
        }
    }

    public class ListboxEx : ListBox
    {
        int IconWidth
        { get; set; }

        int IconHeight
        { get; set; }

        ToolTip mTip;

        public static string[] TypeString = {
                                         "[未知]",
                                         "[文件]",
                                         "[文本]",
                                         "[目录]"
                                     };

        public static Image[] Images = {
                                           global::ClipboardEx.Properties.Resources.png_unknow,
                                           global::ClipboardEx.Properties.Resources.png_file,
                                           global::ClipboardEx.Properties.Resources.png_text,
                                           global::ClipboardEx.Properties.Resources.png_dir
                                       };

        public ListboxEx()
        {
            DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
            mTip = new ToolTip();

            IconWidth = 24;
            IconHeight = 24;
        }

        private void setTipMessage(string strTip)
        {
            // 不同才设置
            if (mTip.GetToolTip(this) != strTip)
            {
                mTip.SetToolTip(this, strTip);
            }
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);

            int index = IndexFromPoint(e.Location);
            if (index == -1)
            {
                setTipMessage("");
                return;
            }

            ListItemEx lie = (ListItemEx)this.Items[index];
            string txt = lie.Name;
            txt = txt.Insert(0, lie.Type + "\n");
            if (txt.Length > 512)
            {
                int txtLen = txt.Length;
                txt = txt.Substring(0, 512);
                txt += "...\n文本长度：";
                txt += txtLen.ToString();
            }
            setTipMessage(txt);
        }

        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            e.DrawBackground();
            e.DrawFocusRectangle();

            ItemHeight = IconHeight;

            if (e.Index >= 0 && Items.Count > 0)
            {
                Graphics g = e.Graphics;
                StringFormat sf = new StringFormat();
                sf.Trimming = StringTrimming.EllipsisCharacter;
                sf.LineAlignment = StringAlignment.Center;

                ListItemEx item = (ListItemEx)Items[e.Index];

                SizeF size = g.MeasureString(item.Name, e.Font);
                RectangleF rectF = new RectangleF(e.Bounds.Left + IconWidth,
                    e.Bounds.Top,
                    e.Bounds.Width - IconWidth, IconHeight);

                if (item.ItemImage != null)
                {
                    g.DrawImage(item.ItemImage, e.Bounds.Left,
                        e.Bounds.Top, IconWidth, IconHeight);
                }
                g.DrawString(item.Name, e.Font, new SolidBrush(e.ForeColor), rectF, sf);
            }

            base.OnDrawItem(e);
        }
    }
}
